[Android] 利用PHP傳資料給MySQL

Android傳資料有許多種方法,可以利用Java的方式、PHP等等,這邊介紹的是PHP的方式。

另外,Android現在版本運用到網路技術的都必須使用Thread

權限

首先,先在AndroidManifest.xml加入此行

1
<uses-permission android:name="android.permission.INTERNET" />

Java

建立執行緒
1
2
3
4
5
6
7
Thread t = new Thread(new sendPostRunnable(
string_username,
string_password,
string_name,
string_school,
string_job));
t.start();
Handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg){
switch (msg.what) {
case REFRESH_DATA:
String result = null;
if (msg.obj instanceof String)
result = (String) msg.obj;
if (result != null)
Toast.makeText(LoginPage_Register.this, result, Toast.LENGTH_LONG).show();
break;
}
}
};
HttpPost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
private String uriAPI = "PHP位置";
private String sendPostDataToInternet(String s1 ,String s2 , String s3 , String s4 , String s5){
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", s1));
params.add(new BasicNameValuePair("password", s2));
params.add(new BasicNameValuePair("name", s3));
params.add(new BasicNameValuePair("school", s4));
params.add(new BasicNameValuePair("job", s5));
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String strResult = EntityUtils.toString(httpResponse.getEntity());
return strResult;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
class sendPostRunnable implements Runnable
{
String s1 = null;
String s2 = null;
String s3 = null;
String s4 = null;
String s5 = null;
public sendPostRunnable(String s1 ,String s2 , String s3 , String s4 , String s5){
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.s4 = s4;
this.s5 = s5;
}
@Override
public void run() {
String result = sendPostDataToInternet(s1,s2,s3,s4,s5);
mHandler.obtainMessage(REFRESH_DATA, result).sendToTarget();
}
}

HP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
header("Content-type: text/html; charset=utf-8");
$conn = new mysqli('伺服器位置', '使用者名稱', '使用者密碼', '資料庫名稱');
$conn->set_charset('utf8');
$username = $_POST['username'];
$password = $_POST['password'];
$name = $_POST['name'];
$school = $_POST['school'];
$job = $_POST['job'];
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO 資料表名稱 (username, password, name , school , job)
VALUES ('$username', '$password', '$name' , '$school' , '$job')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

每次處理資料一旦遇到中文就一定會遇上編碼問題,尤其跨了很多方式還得先找出是哪邊的問題,最後發現是在PHP,找了好久才找到可行的方法,但還是得記得所有的過程都要設定為UTF-8

1
$conn->set_charset('utf8');

由於要知道究竟是哪邊有問題,連取資料都先做了,發現使用JSON也會有亂碼,下次介紹取資料的時候在補充。

參考

[Android] 使用HTTP的POST方式和網頁表單溝通 (加上資料庫、執行緒)